﻿using CaterBll;
using CaterModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaterUI
{
    public partial class FormMemberTypeInfo : Form
    {
        public FormMemberTypeInfo()
        {
            InitializeComponent();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private int selectedRowIndex = -1;

        private void FormMemberInfo_Load(object sender, EventArgs e)
        {
            loadList();
        }

        MemberTypeInfoBll mtiBll = new MemberTypeInfoBll();

        //设置关闭窗口值
        private DialogResult result=DialogResult.Cancel;
        private void loadList()
        {
            
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = mtiBll.GetList();

            //设置选中行
            if (selectedRowIndex > -1)
            {
                dgvList.Rows[selectedRowIndex].Selected = true;
            }
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtmtitle.Text=="")
            {
                MessageBox.Show("请填写类型名称！");
                return;
            }
            if (txtmdiscount.Text == "")
            {
                MessageBox.Show("请填写折扣！");
                return;
            }
            if (Convert.ToDecimal(txtmdiscount.Text.Trim())>1)
            {
                MessageBox.Show("折扣不能大于1！");
                return;
            }

            if (txtmid.Text=="添加时无编号")
            {
                MemberTypeInfo mti = new MemberTypeInfo()
                {
                    MTitle = txtmtitle.Text,
                    MDiscount =Convert.ToDecimal(txtmdiscount.Text)
                };

               if( mtiBll.Insert(mti)>0)
                {
                    loadList();
                    clearText();
                }
            }
            else
            {
                MemberTypeInfo mti = new MemberTypeInfo()
                {
                    MId=Convert.ToInt32(txtmid.Text),
                    MTitle = txtmtitle.Text,
                    MDiscount = Convert.ToDecimal(txtmdiscount.Text),
                    MIsDelete=false
                };
                if (mtiBll.Update(mti) > 0)
                {
                    loadList();
                    clearText();
                }
            }

            //设置返回值
            result = DialogResult.OK;
        }

        private void clearText()
        {
            txtmid.Text = "添加时无编号";
            txtmtitle.Text = "";
            txtmdiscount.Text = "";
            btnSave.Text = "添加";
        }

        private void dgvList_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           

        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedRowIndex = e.RowIndex;

            DataGridViewRow row = dgvList.Rows[e.RowIndex];
            txtmid.Text = row.Cells[0].Value.ToString();
            txtmtitle.Text = row.Cells[1].Value.ToString();
            txtmdiscount.Text = row.Cells[2].Value.ToString();
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            clearText();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            //删除前确认
            DialogResult result = MessageBox.Show("你确定要删除吗？", "删除确认", MessageBoxButtons.OKCancel);
            if (result == DialogResult.Cancel)
            {
                return;
            }
            //获取选中的行
            var rows = dgvList.SelectedRows;
            if (rows.Count > 0)
            {
                int mid = int.Parse(rows[0].Cells[0].Value.ToString());
                string MTitle = rows[0].Cells[1].Value.ToString();
                decimal MDiscount =Convert.ToDecimal(rows[0].Cells[2].Value.ToString());
                MemberTypeInfo mti = new MemberTypeInfo();
                mti.MId = mid;
                mti.MTitle = MTitle;
                mti.MDiscount = MDiscount;
                mti.MIsDelete = true;

                if (mtiBll.Update(mti)>0)
                {
                    loadList();
                }
            }
            else
            {
                MessageBox.Show("请先选择要删除的行！");
            }

            //设置返回值
            result = DialogResult.OK;
        }

        private void FormMemberTypeInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = result;
        }
    }
}
